package cemetery.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import com.alibaba.fastjson.JSON;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cemetery.bean.Page;
import cemetery.bean.materiel;
import cemetery.service.MeterielService;
import cemetery.service.impl.MeterielServiceImpl;

/**
 * Servlet implementation class MeterielServlet
 */
@WebServlet("/MeterielServlet")
public class MeterielServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MeterielServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//action 是请求的操作类别。
		String action=request.getParameter("action");
		MeterielService newsService=new MeterielServiceImpl();
		if (action==null || "list".equals(action)) {
			//从请求中拿到数据
			//页码
			Page page=new Page();
			String pageIndexStr=request.getParameter("page");
			int pageIndex=1;
			if (pageIndexStr!=null) {
				pageIndex=Integer.parseInt(pageIndexStr);
			}
			//页面大小
			String pageSizeStr=request.getParameter("limit");
			int pageSize=5;
			if (pageSizeStr!=null) {
				pageSize=Integer.parseInt(pageSizeStr);
			}
			materiel news=new materiel();
			//调用业务逻辑层的方法
			int totalCount=newsService.getTotalCount(news);
			//获取分页数据
			List<materiel>pageList=	newsService.setPageList(news, pageIndex, pageSize);
			//页面的跳转和数据的设备
			Map<String, Object>map=new HashMap<>();
			if (pageList!=null) {
			map.put("success", true);
			map.put("code",0);
			map.put("count",totalCount);
			map.put("data",pageList);
		}else {
			map.put("code",-1);
			map.put("msg","删除失败");
		}
		String result=JSON.toJSONString(map);
		PrintWriter pw=response.getWriter();
		pw.write(result);
		pw.flush();
		pw.close();
		}
		if ("add".equals(action)) {
			//从请求中拿到数据
		String name=	request.getParameter("name");
		String number= request.getParameter("number");
		String science=	request.getParameter("science");
		String sourc=	request.getParameter("sourc");
		String pprice=	request.getParameter("pprice");
		String price=	request.getParameter("price");
		String personincharge=	request.getParameter("personincharge");
			//调用业务逻辑层的方法
		materiel n=new materiel();
		n.setName(name);
		n.setNumber(Integer.parseInt(number));
		n.setScience(science);
		n.setSourc(sourc);
		n.setPprice(Integer.parseInt(pprice));
		n.setPrice(Integer.parseInt(price));
		n.setPersonincharge(personincharge);
		int count=newsService.add(n);
		Map<String, Object>map=new HashMap<>();
		if (count>0) {
		map.put("code", 0);
		map.put("msg","成功");
		}else {
		map.put("code",-1);
		map.put("msg","删除失败");
		}
		String result=JSON.toJSONString(map);
		PrintWriter pw=response.getWriter();
		pw.write(result);
		pw.flush();
		pw.close();
		}
		
		if ("getNewsById".equals(action)) {
			String nidStr=request.getParameter("id");
			materiel news=new materiel();
			news.setId(Integer.parseInt(nidStr));
			List<materiel>list=newsService.getList(news);
			Map<String, Object>map=new HashMap<>();
			if (list!=null&&list.size()>0) {
			map.put("code", 0);
			map.put("msg","成功");
			map.put("data",list.get(0));
			}else {
			map.put("code",-1);
			map.put("msg","修改失败");
			}
			String result=JSON.toJSONString(map);
			PrintWriter pw=response.getWriter();
			pw.write(result);
			pw.flush();
			pw.close();
			}
		if ("update".equals(action)) {
			//从请求中拿到数据
		String name=	request.getParameter("name");
		String number= request.getParameter("number");
		String science=	request.getParameter("science");
		String sourc=	request.getParameter("sourc");
		String pprice=	request.getParameter("pprice");
		String price=	request.getParameter("price");
		String personincharge=	request.getParameter("personincharge");
		String id=	request.getParameter("id");
			//调用业务逻辑层的方法
		materiel n=new materiel();
		n.setName(name);
		n.setNumber(Integer.parseInt(number));
		n.setScience(science);
		n.setSourc(sourc);
		n.setPprice(Integer.parseInt(pprice));
		n.setPrice(Integer.parseInt(price));
		n.setPersonincharge(personincharge);
		n.setId(Integer.parseInt(id));
		int count=newsService.update(n);
		Map<String, Object>map=new HashMap<>();
		if (count>0) {
		map.put("code", 0);
		map.put("msg","成功");
		}else {
		map.put("code",-1);
		map.put("msg","删除失败");
		}
		String result=JSON.toJSONString(map);
		PrintWriter pw=response.getWriter();
		pw.write(result);
		pw.flush();
		pw.close();
		}
		if ("delete".equals(action)) {
			String id=request.getParameter("id");
			int count=newsService.deleteById(Integer.parseInt(id));
			Map<String, Object>map=new HashMap<>();
			if (count>0) {
			map.put("code", 0);
			map.put("msg","成功");
			}else {
			map.put("code",-1);
			map.put("msg","删除失败");
			}
			String result=JSON.toJSONString(map);
			PrintWriter pw=response.getWriter();
			pw.write(result);
			pw.flush();
			pw.close();
			}
		}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
